<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class EbayMessage_m extends CI_Model {
    
	function __construct(){
        parent::__construct();
        
    }
    
    public function getShopMessage($shopid,$messageid=0){
        ini_set('max_execution_time', '0');
        $this->load->library('EbayMessageApi',null,'ebayapi');
        $row = $this->db->get_where("d_shop","id={$shopid}")->result_array();//获取店铺token
        foreach ($row as $v) {
            $shopid=$v['id'];
            $name=$v['name'];
            $data=json_decode($v['amazonkey'],true);
            $devID=$data['ebaydevid'];
            $userToken=$data['ebayusertoken'];
            $appID=$data['ebayappid'];
            $certID=$data['ebaycertid'];
            $siteID=$data['ebaysiteid'];
            $compatabilityLevel=$data['ebaycompatabilitylevel'];
        }
        if($messageid>0){
            $messageids[]=$messageid;
            $this->ebayapi->importMessageDetail($userToken, $devID, $appID, $certID, $compatabilityLevel, $siteID,$messageids);    
        }else{
            $messagexml=$this->ebayapi->apiCallMessage($userToken, $devID, $appID, $certID, $compatabilityLevel, $siteID,$shopid);
            echo "<br />消息导入完成；<br />";    
        }
    }
    
    
    public function updateOnlineReply($replyinfo){
        ini_set('max_execution_time', '0');
        $shopid=$replyinfo['msg_info']['shop_id'];
        $this->load->library('EbayMessageApi',null,'ebayapi');
        $row = $this->db->get_where("d_shop","id={$shopid}")->result_array();//获取店铺token
        foreach ($row as $v) {
            $shopid=$v['id'];
            $name=$v['name'];
            $data=json_decode($v['amazonkey'],true);
            $devID=$data['ebaydevid'];
            $userToken=$data['ebayusertoken'];
            $appID=$data['ebayappid'];
            $certID=$data['ebaycertid'];
            $siteID=$data['ebaysiteid'];
            $compatabilityLevel=$data['ebaycompatabilitylevel'];
        }
        $messagexml=$this->ebayapi->apiCallReply($userToken, $devID, $appID, $certID, $compatabilityLevel, $siteID,$replyinfo); 
        return $messagexml;
    }
	  public function addEbayBill(){
	    $data = array();
	  $id = trim($this->input->post("ebayId"));
	  $arr_excel_file = $_FILES['file_excel'];
	  if(!empty($arr_excel_file)&&$arr_excel_file['error']==0) {
		  $fileName = $arr_excel_file["name"];
		  //$fileType = $arr_excel_file["type"];
		  //$fileSize = $arr_excel_file["size"];
		  $tmpFileName = $arr_excel_file["tmp_name"];
		  //扩展名判断
		  $fileExtName = explode(".", $fileName);
		  $extName = $fileExtName[count($fileExtName)-1];
		  if (!($extName == "xls" || $extName == "xlsx" || $extName =='html')) {
			  echo "上传的文件扩展名应该为.xls或.xlsx或.csv";
			  exit;
		  }
		  $path = dirname(__FILE__)."/../../uploads/ebaybill/".date("Ymd")."/";
		  $rela_path = "/uploads/ebaybill/".date("Ymd")."/";
		  @!is_dir($path)&&mkdir($path,0755,true);
		  $rand_file_name = date("YmdHis").rand(111111,999999).".".$extName;
		  move_uploaded_file($tmpFileName, $path.$rand_file_name);
		  $data['excelUrl']= $rela_path.$rand_file_name;
		  if($id){
			  $ebayBill = $this->fun->getdatabasesdetail("db_ebay_bill"," AND id = '{$id}' ");
			  if($ebayBill['excelUrl']){
				  $excel_url = dirname(__FILE__)."/../..".$ebayBill['excelUrl'];
				  @unlink($excel_url);
			  }
		  }
	  }
		//当前汇率
	    $rate = $this->fun->getdatabasesdetail("d_moneyrate"," AND name = 'USD' ","rate");

		$data['shopId'] = trim($this->input->post("shopid"));
	    $shop = $this->fun->getdatabasesdetail("d_shop"," AND id = '{$data['shopId']}' ","name");
		$data['shopName'] = $shop['name'];
	    $data['startDate'] = trim($this->input->post("startdate"));
	    $data['endDate'] = trim($this->input->post("enddate"));
		$data['moneyRate'] = $rate['rate'];
		$data['types'] = 1;
		$data['moneyType'] = "USD";
	    $data['insertionFee'] = trim($this->input->post("fee"));
	  $data['insertionFeeRMB'] = trim($this->input->post("fee"))*$rate['rate'];
	    $data['shopfee'] = trim($this->input->post("shopfee"));
	  $data['shopfeeRMB'] = trim($this->input->post("shopfee"))*$rate['rate'];
	    $data['createId'] = $_COOKIE['userid'];
	    $data['createName'] = $_COOKIE['username'];

	    if($id){
			$this->db->update("db_ebay_bill",$data," id = '".$id."'");
	    }else{
		    $data['addTime'] = date("Y-m-d H:i:s");
		    $this->db->insert("db_ebay_bill",$data);
	    }
	    return true;
  }
	
    
    

    
    public function importMessage($devID,$userToken,$appID,$certID,$siteID,$compatabilityLevel){
       // $this->ebayapi->test();
    }
}